package com.lucky.apple.test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by he on 2018/5/8.
 */
public class test2 {
    public static void main(String[] args) {
        int[] ints = new int[]{1024,8192,10240,102400,1024000};
        Stack<mytest> stack = new Stack<>();
        stack.push(new mytest(0,ints[0]));
        stack.push(new mytest(1,ints[1]));
        stack.push(new mytest(2,ints[2]));
        stack.push(new mytest(3,ints[3]));
        stack.push(new mytest(4,ints[4]));

        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            pool.submit(()->{
                mytest pop = stack.pop();
                havebuffer(pop.getValue(), pop.getKey());
            });
        }
        pool.shutdown();
    }

    public static void havebuffer(int size, int num){
        File file = new File("D:\\work\\personal\\mytest\\readTest"+num+".txt");
        File outFile = new File("D:\\work\\personal\\mytest\\out"+num+".txt");
        BufferedInputStream stream1 = null;
        BufferedOutputStream stream2 = null;
        try {
            long startTime = System.currentTimeMillis();
            stream1 = new BufferedInputStream(new FileInputStream(file));
            stream2 = new BufferedOutputStream(new FileOutputStream(outFile));
            byte[] bytes = new byte[size];
            int len = 0;
            while ((len = stream1.read(bytes)) > 0) {
                stream2.write(bytes,0,len);
            }
            long endTime = System.currentTimeMillis();
            System.out.println(size + " cost: " + (endTime - startTime)+"ms");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                stream2.close();
                stream1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static class mytest{
        private Integer key;
        private Integer value;

        public mytest() {
        }

        public mytest(Integer key, Integer value) {
            this.key = key;
            this.value = value;
        }

        public Integer getKey() {
            return key;
        }

        public void setKey(Integer key) {
            this.key = key;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }
}
